import type { UserInfoResult } from '~/apis/user/model'

export interface LGUserState {
  token: string
  userInfo: UserInfoResult | null
}

export const useUserStore = defineStore('user', {
  state: (): LGUserState => ({
    token: '',
    // 用户信息
    userInfo: null
  }),
  actions: {
    load() {
      this.token = localStorage.getItem('token') || ''
      const userInfo = localStorage.getItem('userInfo')
      this.userInfo = userInfo ? JSON.parse(userInfo) : null
    },
    setToken(val: string) {
      this.token = val
      localStorage.setItem('token', val)
    },
    setUserInfo(val: UserInfoResult) {
      this.userInfo = val
      localStorage.setItem('userInfo', JSON.stringify(val))
    },
    logout() {
      this.token = ''
      this.userInfo = null
      localStorage.removeItem('token')
      localStorage.removeItem('userInfo')
    }
  }
})
